#include "rfid_com.h"

int32_t emv_poll(void)
{
	return rfid_poll(ISO14443_TYPEA | ISO14443_TYPEB);
}

int32_t emv_collision(void)
{
	return rfid_anticollision();
}

int32_t emv_activation(void)
{
	return rfid_active();
}

int32_t emv_comm(uint8_t* command_apdu, uint32_t command_length, uint8_t* response_apdu, uint32_t* response_length)
{
	return rfid_comm(command_apdu, command_length, response_apdu, response_length);
}

int32_t emv_removal(void)
{
	return rfid_removal();
}

static const uint8_t emv_select_PPSE[] = {0x00, 0xA4, 0x04, 0x00, 0x0E, 0x32, 0x50, 0x41, 0x59, 0x2E, 0x53, 0x59, 0x53, 0x2E,
							0x44, 0x44, 0x46, 0x30, 0x31, 0x00};	// PPSE	"2PAY.SYS.DDF01", last 0x00 is Le
int32_t emv_loopback(void)
{
	int32_t status = RFID_UNKNOWN_ERROR;
	uint8_t command[256], response[256];
	uint32_t command_len, response_len;

	do {
		rt_debug_start_print();
		status = emv_poll();
		if (status != RFID_NO_ERROR)	break;
		rt_debug_add_print("====>poll ok! \r\n");
		status = emv_collision();
		if (status != RFID_NO_ERROR) { rfid_reset_antenna(CONFIG_WAIT_TRESET); break;}
		rt_debug_add_print("====>collision ok! \r\n");
		status = emv_activation();
		if (status != RFID_NO_ERROR) { rfid_reset_antenna(CONFIG_WAIT_TRESET); break;}
		rt_debug_add_print("====>active ok! \r\n");
		command_len = sizeof(emv_select_PPSE);
		memcpy(command, emv_select_PPSE, command_len);
		while (1) {
			response_len = 256;
			status = emv_comm(command, command_len, response, &response_len);
			if ((status != RFID_NO_ERROR) || (response_len <= 3) || (response[1] == 0x70) || (response[1] == 0x72)) {
				break;
			}
			command_len = response_len - 2;
			memcpy(command, response, command_len);
		}
		if (response[1] == 0x72) {
			rfid_reset_antenna(CONFIG_WAIT_ANALOG_TRESET);
			continue;
		}
		rt_debug_add_print("====>removal \r\n\r\n");
		rt_debug_end_print();
		emv_removal();
		rfid_delay_ms(CONFIG_WAIT_TPAUSE);
	} while (0);
	rt_debug_end_print();
	return status;
}

int32_t emv_transac_a(void)
{
	rfid_set_exception(RFID_NONE_EXCEPTION);	// 都只发送一次

	// WUPA
	rfid_mode_config(ISO14443_TYPEA, RFID_106KBPS);
	rfid_delay_ms(2);
	rfid_transceive_config(RFID_HW_CONFIG_POLLA);	// WUPA 7bit with no CRC
	rfid_transceive("\x52", 0x01, NULL, NULL, 0);
	rfid_transceive_config(RFID_HW_CONFIG_NORMAL);
	rfid_delay_ms(2);
	// HALTA
	rfid_transceive("\x50\x00", 0x02, NULL, NULL, 0);
	rfid_mode_config(ISO14443_TYPEB, RFID_106KBPS);
	rfid_transceive_config(RFID_HW_CONFIG_NORMAL);
	rfid_delay_ms(2);
	// WUPB
	rfid_transceive("\x05\x00\x08", 0x03, NULL, NULL, 0);
	rfid_mode_config(ISO14443_TYPEA, RFID_106KBPS);
	rfid_transceive_config(RFID_HW_CONFIG_POLLA);	// WUPA 7bit with no CRC
	rfid_delay_ms(2);
	// WUPA
	rfid_transceive("\x52", 0x01, NULL, NULL, 0);
	rfid_transceive_config(RFID_HW_CONFIG_ANTICOLLISION);	// SDD 8bit with no CRC
	rfid_delay_ms(2);
	// SDD
	rfid_transceive("\x93\x20", 0x02, NULL, NULL, 0);
	rfid_transceive_config(RFID_HW_CONFIG_NORMAL);
	rfid_delay_ms(2);
	// SELECT
	rfid_transceive("\x93\x70\x27\xe9\x3b\x11\xe4", 0x07, NULL, NULL, 0);
	rfid_delay_ms(2);
	// RATS
	rfid_transceive("\xe0\x80", 0x02, NULL, NULL, 0);
	rfid_delay_ms(2);
	// APDU
	rfid_transceive("\x02\x00\xA4\x04\x00\x0E\x32\x50\x41\x59\x2E\x53\x59\x53\x2E\x44\x44\x46\x30\x31\x00", 21, NULL, NULL, 0);
	rfid_delay_ms(2);
	rfid_transceive("\x03\x00\xA4\x04\x00\x0C\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x00", 19, NULL, NULL, 0);
	return RFID_NO_ERROR;
}

int32_t emv_transac_b(void)
{
	rfid_set_exception(RFID_NONE_EXCEPTION);
	// WUPB
	rfid_mode_config(ISO14443_TYPEB, RFID_106KBPS);
	rfid_transceive_config(RFID_HW_CONFIG_NORMAL);
	rfid_delay_ms(2);
	rfid_transceive("\x05\x00\x08", 0x03, NULL, NULL, 0);
	rfid_mode_config(ISO14443_TYPEA, RFID_106KBPS);
	rfid_transceive_config(RFID_HW_CONFIG_POLLA);	// WUPA 7bit with no CRC
	rfid_delay_ms(2);
	// WUPA
	rfid_transceive("\x52", 0x01, NULL, NULL, 0);
	rfid_mode_config(ISO14443_TYPEB, RFID_106KBPS);
	rfid_transceive_config(RFID_HW_CONFIG_NORMAL);
	rfid_delay_ms(2);
	// WUPB
	rfid_transceive("\x05\x00\x08", 0x03, NULL, NULL, 0);
	rfid_delay_ms(2);
	// ATTRIB
	rfid_transceive("\x1D\x46\xb5\xc7\xa0\x00\x08\x01\x00", 0x09, NULL, NULL, 0);
	rfid_delay_ms(2);
	// APDU
	rfid_transceive("\x02\x00\xA4\x04\x00\x0E\x32\x50\x41\x59\x2E\x53\x59\x53\x2E\x44\x44\x46\x30\x31\x00", 21, NULL, NULL, 0);
	rfid_delay_ms(2);
	rfid_transceive("\x03\x00\xA4\x04\x00\x0C\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x00", 19, NULL, NULL, 0);
	return RFID_NO_ERROR;
}
